package com.thread.threadpractice.service;

import com.alibaba.excel.EasyExcel;
import com.thread.threadpractice.domain.ProductExcel;
import com.thread.threadpractice.entity.Product;
import com.thread.threadpractice.mapper.ProductMapper;
import com.thread.threadpractice.utils.ExcelListener;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.concurrent.CompletableFuture;

@Service
public class ProductService {
    @Resource
    private ProductMapper productMapper;

    public List<Product> selectAll(){
        return productMapper.selectAll();
    }

    public void insertBatch(List<Product> list) {
        productMapper.batchInsert(list);
    }

    @Async("asyncExecutor")
    public CompletableFuture<String> exportBatch(List<Product> productListBatch, String filePath) {
        EasyExcel.write(filePath, Product.class)
                .sheet("产品信息")
                .doWrite(productListBatch);
        return CompletableFuture.completedFuture("完成：" + filePath);
    }

    @Async("asyncExecutor")
    public void insertExcel(File file) throws IOException {
        EasyExcel.read(file, ProductExcel.class, new ExcelListener(this))
                .sheet("产品信息")
                .doRead();
    }
}
